package com.rimi.controller;

import com.rimi.bean.User;
import com.rimi.commons.CommonsDataResult;
import com.rimi.commons.CommonsResult;
import com.rimi.commons.ResultCode;
import com.rimi.commons.ResultStaus;
import com.rimi.commons.vo.UserHeaderVo;
import com.rimi.service.UserService;
import com.rimi.util.DESUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @
 * @author:liangLi
 * @time:2020/7/6 12:47
 */
@CrossOrigin
@Controller
@Api(value = "用户登录" ,description = "用户登录")
public class LoginController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("登录方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户登录的用户名",defaultValue = "null",required = true)
    })
    public ResultCode login(String username, String password, HttpServletRequest request, HttpServletResponse response) {


        User user = new User();

        String token = getToken(password);


        user.setUsername(username);
        user.setPassword(token);

        /**
         * 判断账号是否注册
         */
        User loginWithUsername = userService.loginWithUsername(user);
        if (loginWithUsername==null){
            return CommonsResult.onFailed(ResultStaus.USER_ID_NOT_FOUND);
        }


        /**
         * 通过username和password判断是否查询到用户
         */
        User login = userService.login(user);


        if (login==null){
          return  CommonsResult.onFailed(ResultStaus.WRONG_USERNAME_OR_PASSWORD);
        }

//        使用cookie监听用户
        Cookie cookie = new Cookie("Id", login.getUserId().toString());
        cookie.setPath("/");
        response.addCookie(cookie);

//        如果为空，返回失败，否则返回成功
        return login == null ? CommonsResult.onFailed(ResultStaus.LOGIN_FAILED) : CommonsResult.onSuccess();
    }

    @GetMapping("/nick")
    @ApiOperation("返回头像和昵称的接口")
    @ResponseBody
    public CommonsDataResult nickname(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");

        UserHeaderVo user1 = new UserHeaderVo();

        user1.setNickname(user.getNickname());
        user1.setUserHeadImage(user.getUserHeadImage());

        //状态码的调整 成功和失败
        if (user1==null){
            return CommonsDataResult.onFailed(ResultStaus.EXCEPTION_ERROR);
        }else {
            //成功就显示头像和昵称和状态码信息
            return CommonsDataResult.onSuccess(user1);
        }

    }

    /**
     * 加密密码
     * @param string
     * @return
     */
    public static String getToken(String string){
        return DESUtil.encode(string);
    }




    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }
    @GetMapping("/welcome")
    public String welcome(){
        return "welcome";
    }
}
